package net.chuyue.mouseEventPlus
{
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	public class MouseEventPlus
	{
		public function MouseEventPlus()
		{
			throw new Error("MouseEventPlus not need being Structured");
		}
		
		
		static public const	DOUBLE_CLICK:String = "MouseEventPlus:doubleclick";
		static public const	CLICK:String = "MouseEventPlus:click";
		static public const	CLICK_WITHOUT_DRAG:String = "MouseEventPlus:quick click without move between mouse up and down||to be fix";
		
		
		static private var _inited			:Boolean = false;
		
		static private var _targetsByTimer	:Dictionary;
		static private var _timersByTarget	:Dictionary;
		
		static private var _lastMouseClickEvents	:Dictionary;
		
		static private var _lastMouseUpEvents		:Dictionary;
		
		
		static public function registerTarget(target_:InteractiveObject,limitTime_:Number = 180):void{
			//	init
			if(!_inited)_init();
			
			
			//	do register
			
			var timer:Timer = new Timer(limitTime_,1);
			_timersByTarget[target_] = timer;
			_targetsByTimer[timer] = target_;
			
			target_.addEventListener(MouseEvent.CLICK,_onTargetClick);
			timer.addEventListener(TimerEvent.TIMER,_onTimer);
			
			target_.addEventListener(MouseEvent.MOUSE_UP,_onTargetMouseUp);
			target_.addEventListener(MouseEvent.MOUSE_DOWN,_onTargetMouseDown);
		}
		
		static public function removeTarget(target_:InteractiveObject):void{
			//[[	to be done
		}
		
		
		
		static private function _init():void{
			if(!_targetsByTimer)_targetsByTimer = new Dictionary();
			if(!_timersByTarget)_timersByTarget = new Dictionary();
			if(!_lastMouseClickEvents)_lastMouseClickEvents = new Dictionary();
			if(!_lastMouseUpEvents)_lastMouseUpEvents = new Dictionary();
			
			_inited = true;
		}
		
		static private function _onTargetClick(e:MouseEvent):void{
			var target:InteractiveObject = e.currentTarget as InteractiveObject;
			var timer:Timer = _timersByTarget[target];
			_lastMouseClickEvents[target] = e;
			if(timer.running){
				timer.stop();
				var mouseDClick:MouseEvent = new MouseEvent(MouseEventPlus.DOUBLE_CLICK,e.bubbles,e.cancelable,e.localX,e.localY,e.relatedObject,e.ctrlKey,e.altKey,e.shiftKey,e.buttonDown,e.delta);
				target.dispatchEvent(mouseDClick);
			}else{
				timer.start();
			}
		}
		static private function _onTimer(e:TimerEvent):void{
			var timer:Timer = e.currentTarget as Timer;
			var target:InteractiveObject = _targetsByTimer[timer];
			var lastEvent:MouseEvent = _lastMouseClickEvents[target];
			var mouseClick:MouseEvent = new MouseEvent(MouseEventPlus.CLICK,lastEvent.bubbles,lastEvent.cancelable,lastEvent.localX,lastEvent.localY,lastEvent.relatedObject,lastEvent.ctrlKey,lastEvent.altKey,lastEvent.shiftKey,lastEvent.buttonDown,lastEvent.delta);
			target.dispatchEvent(mouseClick);
		}
		
		
		static private function _onTargetMouseDown(e:MouseEvent):void{
			var target:InteractiveObject = e.currentTarget as InteractiveObject;
			_lastMouseUpEvents[target] = e;
		}
		static private function _onTargetMouseUp(e:MouseEvent):void{
			var target:InteractiveObject = e.currentTarget as InteractiveObject;
			var lastMouseUpEvent:MouseEvent = _lastMouseUpEvents[target];
			
			if(lastMouseUpEvent && lastMouseUpEvent.localX == e.localX && lastMouseUpEvent.localY == e.localY){
				var mouseClickWithoutDrag:MouseEvent = new MouseEvent(MouseEventPlus.CLICK_WITHOUT_DRAG,e.bubbles,e.cancelable,e.localX,e.localY,e.relatedObject,e.ctrlKey,e.altKey,e.shiftKey,e.buttonDown,e.delta);
				target.dispatchEvent(mouseClickWithoutDrag);
			}
		}
		
	}
}